<template>
	<view>
		<u-navbar
			title="申请退款"
			:autoBack="true"
			placeholder
		>
		</u-navbar>
		<scroll-view :scroll-y="true" :style="{height: scrollViewHeight}">
			<template v-if="loading">
				<view style="padding: 32rpx;">
					<u-skeleton style="margin-bottom: 32rpx;" :loading="loading" avatar rows="7" v-for="i in 8" :key="i"></u-skeleton>
				</view>
			</template>
			<template v-else>
				<view class="shop-cart-box">
					<view class="shop-cart-r">
						<u--image radius="16rpx" mode="aspectFill" :src="detail.image[0].url" width="140rpx" height="140rpx"></u--image>
						<view style="width: 16rpx;"/>
						<view class="shop-cart-r-1">
							<view class="shop-cart-r-1-title u-line-1">{{dataList[0].name.split('#')[0]}}</view>
							<view class="shop-cart-r-2-title u-line-1">{{dataList[0].adoptSku}}</view>
							<view style="height: 32rpx;"/>
							<view class="shop-cart-r-1-con">
								<text style="color: #f40215;font-size: 28rpx;">&yen;{{dataList[0].price}}</text>
								<text style="color: #666;font-size: 24rpx;">x{{dataList.length}}</text>
								<!-- <u-number-box :asyncChange="true" @change="onChange(goods, $event)" :min="0" :max="99" :buttonSize="24" disabledInput v-model="goods.buyCount"></u-number-box> -->
							</view>
						</view>
					</view>
				</view>
				<view style="height: 16rpx;"/>
				<view class="logistics-box">
					<view class="logistics-title" style="padding-bottom: 16rpx;">
						<text class="logistics-title-l">订单编号:</text>
						<text class="logistics-title-r">{{detail.orderNo}}</text>
					</view>
					<view class="logistics-title">
						<text class="logistics-title-l">交易时间:</text>
						<text class="logistics-title-r">{{detail.createTime}}</text>
					</view>
				</view>
				<view style="height: 16rpx;"/>
				<u--form labelPosition="left" :model="model" ref="form1">
					<view style="padding: 0 32rpx;background-color: #fff;">
						<u-form-item @click="showReason = true" labelWidth="80" label="退款原因" borderBottom ref="refundReason">
							<u--input disabled disabledColor="#ffffff" placeholder="请选择退款原因" v-model="model.refund.refundReason" border="none"></u--input>
							<u-icon slot="right" name="arrow-right"></u-icon>
						</u-form-item>
						<u-form-item labelWidth="80" label="预付金额" borderBottom ref="refundPrice" v-if="refundType == 1">
							<text style="color: #f40215;">{{ detail.payPrice }}元</text>
						</u-form-item>
						<u-form-item labelWidth="80" label="违约金额" borderBottom ref="refundPrice">
							<text style="color: #f40215;">-{{ detail.depositRefundPrice }}元</text>
						</u-form-item>
						<u-form-item labelWidth="80" label="退款金额" borderBottom ref="refundPrice">
							<text style="color: #f40215;">{{ detail.refundPrice }}元</text>
						</u-form-item>
						<u-form-item labelWidth="80" label="手机号码" borderBottom ref="phone">
							<u--input type="number" placeholder="请输入手机号码" v-model="model.refund.phone" border="none">
							</u--input>
						</u-form-item>
						<u-form-item labelWidth="80" label="备注信息" borderBottom ref="remark">
							<u--input placeholder="请输入备注信息" v-model="model.refund.remark" border="none"></u--input>
						</u-form-item>
						<u-form-item labelWidth="160" label="上传凭证,最多3张" ref="img">
						</u-form-item>
						<u-upload
								:fileList="fileList1"
								@afterRead="afterRead"
								@delete="deletePic"
								name="1"
								multiple
								:maxCount="3"
							></u-upload>
					</view>
				</u--form>
			</template>
		</scroll-view>
		<view class="good-detail-bootom" v-if="!loading">
			<view style="margin: 0 32rpx;height: 100%;width: calc(100vw - 64rpx);display: flex;align-items: center;justify-content: space-between;">
				<view class="good-detail-bootom-r">
					<u-button @click="submit" :color="appColor" text="提交申请" shape="circle"></u-button>
				</view>
			</view>	
		</view>
		<u-action-sheet
				:show="showReason"
				:actions="actions"
				title="请选择退款原因"
				@close="showReason = false"
				@select="refundReasonSelect"
		>
		</u-action-sheet>
	</view>
</template>

<script>
	import {mapState, mapActions} from 'vuex';
	import {goodsAfterOrder} from '@/api/shop.js';
	import {getToken} from '@/utils/auth.js';
	import {base_url} from '@/utils/request.js';
	import {getDictItems, getOrderCount, getRefundPrice, refundApply} from '@/api/me.js';
	export default {
		data() {
			return {
				dataList: [],
				loading: true,
				actions: [],
				showReason: false,
				model: {
					refund: {
						refundReason: undefined,
						phone: undefined,
						remark: '',
					},
				},
				fileList1: [],
				detail: {},
				orderId: undefined,
				refundType: -1
			}
		},
		computed: {
			...mapState(['appInfo']),
			appColor() {
				return this.appPrimaryColor
			},
			scrollViewHeight() {
				return this.scrollHeight - 44 - 50 + 'px';
			},
		},
		onLoad({id, refundType}) {
			this.orderId = id;
			this.refundType = refundType;
			this.loading = true;
			// 1 定金 2 尾款
			getRefundPrice({orderId: id, refundType}).then(res => {
				this.detail = res.result;
				this.dataList = res.result.detailList;
			}).finally(() => {
				this.loading = false;
			})
			getDictItems('goods_order_refund_reason').then(res => {
				this.actions = res.result
			})
		},
		methods: {
			...mapActions(['setOrderCount']),
			// 获取数量
			getOrderCount() {
				getOrderCount().then(res => {
					this.setOrderCount({orderCount: res.result});
				})
			},
			// 改变选中
			changeChecked(goods, index) {
				let cur = uni.$u.deepClone(goods);
				cur.isChecked = !cur.isChecked;
				this.dataList.splice(index, 1, cur);
			},
			// 提交
			submit() {
				let {refundReason, phone, remark} = this.model.refund;
				remark = uni.$u.trim(remark);
				phone = uni.$u.trim(phone);
				if(!refundReason) {
					return this.$cuTip('请选择退款原因');
				}
				let refundReasonVal = this.actions.find(item => item.name === refundReason).value
				if(!uni.$u.test.mobile(phone)) {
					return this.$cuTip('请输入正确的手机号');
				}
				if(!remark) {
					return this.$cuTip('请输入备注信息');
				}
				let postObj = {
					orderId: this.orderId,
					refundReason: refundReasonVal,
					phone,
					remark,
					image: this.fileList1,
					refundType: this.refundType
				};
				this.$cuLoading().then(() => {
					refundApply(postObj).then(res => {
						uni.hideLoading();
						const {code, message} = res;
						if(code === 500) {
							this.$cuTip(message);
						} else {
							this.$cuTip('提交成功').then(() => {
								uni.redirectTo({
									url: '/pages/me/afterSaleFoster'
								})
							});
						}
						
					}).catch(() => {
						uni.hideLoading();
					})
				})
				console.log(postObj);
			},
			refundReasonSelect(e) {
				this.model.refund.refundReason = e.name;
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: base_url + '/jeecg-boot/sys/common/upload', // 仅为示例，非真实的接口地址
						header: {
							'content-type': 'application/json',
							'Login-Type': 'MEMBER',
							'X-Access-Token': getToken(),
						},
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(JSON.parse(res.data).message)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style>
	page {
		background-color: #f5f5f5;
	}
</style>

<style lang="scss" scoped>
	.shop-cart-box {
		background-color: #fff;
		padding: 32rpx;
		display: flex;
		align-items: center;
		.shop-cart-l {
			display: flex;
			align-items: center;
			width: 72rpx;
		}
		.shop-cart-r {
			flex: 1;
			display: flex;
			.shop-cart-r-1 {
				flex: 1;
				.shop-cart-r-1-title {
					font-size: 28rpx;
					color: #333;
				}
				.shop-cart-r-2-title {
					font-size: 24rpx;
					color: #666;
				}
				.shop-cart-r-1-con {
					height: 48rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
				}
			}
		}
	}
	
	.logistics-box {
		padding: 32rpx;
		background-color: #fff;
		.logistics-title {
			display: flex;
			align-items: center;
			font-size: 28rpx;
			line-height: 28rpx;
			.logistics-title-l {
				color: #666;
				padding-right: 8rpx;
			}
			.logistics-title-r {
				color: #333;
			}
		}
	}
	
	
	.good-detail-bootom {
		background-color: #ffffff;
		position: fixed;
		height: 50px;
		width: 100vw;
		bottom: 0;
		left: 0;
		padding-bottom: constant(safe-area-inset-bottom);     /*兼容 iOS<11.2*/
		padding-bottom: env(safe-area-inset-bottom);  /*兼容 iOS>11.2*/
		box-sizing: content-box;
		.good-detail-bootom-l1 {
			display: flex;
			flex-direction: column;
			justify-content: center;
			.good-detail-bootom-l1-t {
				font-size: 28rpx;
				padding-bottom: 4rpx;
			}
			.good-detail-bootom-l1-b {
				.good-detail-bootom-l1-b-1 {
					display: flex;
					align-items: center;
					font-size: 24rpx;
				}
			}
		}
		.good-detail-bootom-r {
			flex: 1;
		}
	}
</style>